package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.chart.Bar2D;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Pie2D;
import com.timothy.service.BackLightService;

public class Pie2DAnswerActivity extends BaseAnswerActivity{
	private final static String LOG_TAG = "Pie2DAnswerActivity";
	List<Pie2D> pieList = new ArrayList<Pie2D>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setTitle("Pie2DAnswer");
		
		Log.i(LOG_TAG,"-------------------onCreate-------------------");
		
		for(int i=0;i<chartList.size();i++){
			Log.i(LOG_TAG,"Loop chartList for PieList");
			
			BaseChart baseChart = chartList.get(i);
			Log.i(LOG_TAG,baseChart.toString());
			
			Pie2D pie2D = new Pie2D();
				
			try {
				ValueCopyUtil.valueCopy(baseChart, pie2D);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Log.i(LOG_TAG,pie2D.toString());

			pieList.add(pie2D);
		}
		
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		VerticalPie2DView view = new VerticalPie2DView(this);
		setContentView(view);
	}
	
	class VerticalPie2DView extends SurfaceView implements SurfaceHolder.Callback{
		private SurfaceHolder mHolder;
		private Canvas canvas;
		
		public VerticalPie2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalPie2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalPie2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalPie2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalPie2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalPie2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalPie2DView:bottomM:"+bottomM);
			
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalPie2DView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalPie2DView:queY:"+queY);
			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY/2,mPaint); 
			canvas.save();
			
			int pieWidth = canvasWidth - (int)(setting.getLeftMargin()*1.1 + setting.getRightMargin()*1.1);
			int pieHeight = pieWidth/2;
			
			float startAngle = 0;
			for(int i=0;i<pieList.size();i++){
				Pie2D pie2D = pieList.get(i);
				Log.i(LOG_TAG, "start draw pie2D:"+pie2D.toString());
				
				mPaint.setColor(Color.parseColor(pie2D.getFullColor()));
				int startX = (int)(setting.getLeftMargin()*1.1);
				int startY = (int)(setting.getTopMargin()*1.1);
				int endX = startX + pieWidth;
				int endY = startY + pieHeight;
				
//				RectF recF = new RectF(pie2D.getOvalStartX(),pie2D.getOvalStartY(),pie2D.getOvalStartX()+pie2D.getOvalWidth(),pie2D.getOvalHeight());
				
				RectF recF = new RectF(startX,startY,endX,endY);
				float sweepAngle = new Float(pie2D.getShare()*360) ;
				Log.i(LOG_TAG, "start draw pie2D:"+"startAngle:"+startAngle+" sweepAngle:"+sweepAngle);
				canvas.drawArc(recF, startAngle, sweepAngle, true, mPaint);
				canvas.save();
				
//				double sinValue = Math.sin(startAngle+sweepAngle/2);
//				double cosValue = Math.cos(startAngle+sweepAngle/2);
//				Log.i(LOG_TAG, "textStartY:" + sinValue);
//				Log.i(LOG_TAG, "textStartY:" + cosValue);
//				
//				int centreX = startX + pieWidth/2;
//				int centreY = startY + pieHeight/2;
//				Log.i(LOG_TAG, "centreX:" + centreX);
//				Log.i(LOG_TAG, "centreY:" + centreY);
				
//				float centerAngle = (startAngle + sweepAngle)/2;
//				int radius = pieWidth/4;
//				int centreX = startX + pieWidth/2;
//				int centreY = startY + pieHeight/2;
//				int[] textCoordinates = calTextCoordinates(centreX,centreY,radius,centerAngle);
//				Log.i(LOG_TAG, "textCoordinates[0]:"+textCoordinates[0]);
//				Log.i(LOG_TAG, "textCoordinates[1]:"+textCoordinates[1]);
				
//				//draw choice text
//				mPaint.setTextSize(pie2D.getChoiceTextSize());
//				mPaint.setColor(Color.YELLOW);
//				canvas.drawText(pie2D.getChoiceText()+"("+pie2D.getAnswerText()+")", textCoordinates[0], textCoordinates[1], mPaint);
//				canvas.save();
				
				int textStartY = (canvasHeight/10)* 4; 
				int textEndY = canvasHeight - setting.getBottomMargin();
				int textHeight = (textEndY - textStartY)/10;
				
				int textX = startX; 
				int textY = textStartY+(i+1)*textHeight;
				
				Log.i(LOG_TAG, "textStartY:" + textStartY);
				Log.i(LOG_TAG, "textEndY:" + textEndY);
				Log.i(LOG_TAG, "textHeight:" + textHeight);
				
				Log.i(LOG_TAG, "textX:" + textX);
				Log.i(LOG_TAG, "textY:" + textY);
				
				//draw text line
				mPaint.setColor(Color.WHITE);
				canvas.drawLine(startX, textStartY, textX, textY, mPaint);
				
				//draw text
				mPaint.setTextSize(pie2D.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(pie2D.getFullColor()));
				canvas.drawText(pie2D.getChoiceText()+"("+pie2D.getAnswerText()+")", textX, textY, mPaint);
				canvas.save();
				
				startAngle = startAngle + sweepAngle;
//				canvas.save();
			}

			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);
			
		}
		
		public int[] calTextCoordinates(int centreX,int centreY,int radius,float centerAngle){
			Log.i(LOG_TAG, "-------------start calTextCoordinates-----------");
			
			Log.i(LOG_TAG, "centreX:"+centreX+"centreY:"+centreY);
			Log.i(LOG_TAG, "radius:"+radius);
			Log.i(LOG_TAG, "centerAngle:"+centerAngle);
			
			int disX = (int) (radius * Math.cos(centerAngle));
			int disY = (int) (radius * Math.sin(centerAngle));
			
			Log.i(LOG_TAG, "disX:"+disX);
			Log.i(LOG_TAG, "disY:"+disY);
			
			int[] textCoordinates = new int[]{centreX,centreY};
			textCoordinates[0] = centreX + disX;
			textCoordinates[1] = centreY + disY;
			
			Log.i(LOG_TAG,"-------------end calTextCoordinates-----------");
			return textCoordinates;
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}

